package cn.iocoder.yudao.module.digital.service.competition;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.digital.controller.admin.competition.vo.*;
import cn.iocoder.yudao.module.digital.dal.dataobject.competition.CompetitionDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.digital.dal.mysql.competition.CompetitionMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.digital.enums.ErrorCodeConstants.*;

/**
 * 竞赛 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class CompetitionServiceImpl implements CompetitionService {

    @Resource
    private CompetitionMapper competitionMapper;

    @Override
    public Integer createCompetition(CompetitionSaveReqVO createReqVO) {
        // 插入
        CompetitionDO competition = BeanUtils.toBean(createReqVO, CompetitionDO.class);
        competitionMapper.insert(competition);
        // 返回
        return competition.getId();
    }

    @Override
    public void updateCompetition(CompetitionSaveReqVO updateReqVO) {
        // 校验存在
        validateCompetitionExists(updateReqVO.getId());
        // 更新
        CompetitionDO updateObj = BeanUtils.toBean(updateReqVO, CompetitionDO.class);
        competitionMapper.updateById(updateObj);
    }

    @Override
    public void deleteCompetition(Integer id) {
        // 校验存在
        validateCompetitionExists(id);
        // 删除
        competitionMapper.deleteById(id);
    }

    private void validateCompetitionExists(Integer id) {
        if (competitionMapper.selectById(id) == null) {
            throw exception(COMPETITION_NOT_EXISTS);
        }
    }

    @Override
    public CompetitionDO getCompetition(Integer id) {
        return competitionMapper.selectById(id);
    }

    @Override
    public PageResult<CompetitionDO> getCompetitionPage(CompetitionPageReqVO pageReqVO) {
        return competitionMapper.selectPage(pageReqVO);
    }

}